using System.Collections.Generic;
using I2.Loc;
using UnityEngine;
using UnityEngine.SceneManagement;

public class HidenPakageManager : MonoBehaviour
{
	public static HidenPakageManager instance;

	public PakageSettingsListsHolder listsHolder;

	public GameObject CoinPrefab;

	public int levelCoinsCount;

	public int levelCoinsCollected;

	public List<HidenPakage> newCollectedPakages;

	public PakageSettingsList pakageList;

	private void Awake()
	{
		instance = this;
		newCollectedPakages = new List<HidenPakage>();
		listsHolder.RefreshAllCoins();
	}

	private void Start()
	{
		InitPakages();
	}

	private void CreatePackageList()
	{
	}

	private void InitPakages()
	{
		if ((bool)pakageList)
		{
			PakageSettings[] array = pakageList.packageSettingsList.ToArray();
			PakageSettings[] array2 = array;
			foreach (PakageSettings pakageSettings in array2)
			{
				if (pakageSettings.PakageType == HidenPakageType.Coins)
				{
					levelCoinsCount++;
					if (IsCollected(pakageSettings))
					{
						levelCoinsCollected++;
						continue;
					}
					GameObject gameObject = Object.Instantiate(CoinPrefab, pakageSettings.Position, Quaternion.identity) as GameObject;
					gameObject.transform.SetParent(base.transform);
					gameObject.GetComponent<HidenPakage>().PakageType = pakageSettings.PakageType;
					gameObject.GetComponent<HidenPakage>().PakageIndex = pakageSettings.PakageIndex;
				}
			}
		}
		else
		{
			BCWDebug.LogError("Create pakage list for this level!");
		}
	}

	public bool IsCollected(PakageSettings pakageSettings)
	{
		string key = string.Concat(SceneManager.GetActiveScene().name, " ", pakageSettings.PakageType, " ", pakageSettings.PakageIndex);
		return BCWStorage.Get<bool>(key);
	}

	public void AddToCollectedPakages(HidenPakage pakage)
	{
		newCollectedPakages.Add(pakage);
		if (pakage.PakageType == HidenPakageType.Coins)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Coins_ID", Application.loadedLevelName + "_" + pakage.PakageIndex);
			FlurryWrapper.LogEvent(FlurryWrapper.EV_HIDEN_COINS, dictionary);
			levelCoinsCollected++;
			listsHolder.allCoinsCollected++;
			GameplayEvent gameplayEvent = new GameplayEvent();
			gameplayEvent.offlineXP = 0;
			gameplayEvent.offlineREP = 0f;
			gameplayEvent.pvpXP = 0;
			gameplayEvent.pvpREP = 0f;
			gameplayEvent.rewardType = GameplayRewardType.HiddenCoinCollected;
			gameplayEvent.rewardMessagesSettings = RewardMessages.non;
			gameplayEvent.name = string.Format(ScriptLocalization.GetGameCommonLocalizationFormat("lb_CoinCollected"), listsHolder.allCoinsCollected, listsHolder.allCoinsCount);
			GameplayRewardManager.instance.AddToRewardQueue(gameplayEvent);
			GameplayRewardManager.instance.TryShowMessage();
			settings.ShowLabelWithAnimation(20, pakage.transform.position, true);
			string key = string.Concat(SceneManager.GetActiveScene().name, " ", pakage.PakageType, " ", pakage.PakageIndex);
			BCWStorage.Set(key, true);
			AllUnitaryAchievements.instance.collectHiddenCoins_5.Increment();
			AllUnitaryAchievements.instance.collectHiddenCoins_30.Increment();
		}
	}

	public void SaveAllCollectedPakages()
	{
		foreach (HidenPakage newCollectedPakage in newCollectedPakages)
		{
			string text = string.Concat(SceneManager.GetActiveScene().name, " ", newCollectedPakage.PakageType, " ", newCollectedPakage.PakageIndex);
			BCWDebug.Log("Save bool to Storage wint id - " + text);
			BCWStorage.Set(text, true);
		}
	}

	public void AddAndSaveNewPakages()
	{
		if (!pakageList)
		{
			CreatePackageList();
		}
		if (!pakageList)
		{
			return;
		}
		HidenPakage[] array = Object.FindObjectsOfType<HidenPakage>();
		HidenPakage[] array2 = array;
		foreach (HidenPakage hidenPakage in array2)
		{
			if (hidenPakage.PakageIndex == 0)
			{
				hidenPakage.PakageIndex = GetNewIndex();
				pakageList.AddNewSettings(hidenPakage.PakageType, hidenPakage.PakageIndex, hidenPakage.transform.position);
			}
			Object.DestroyImmediate(hidenPakage.gameObject);
		}
		listsHolder.RefreshAllCoins();
	}

	public void StartEdit()
	{
		if ((bool)pakageList)
		{
			PakageSettings[] array = pakageList.packageSettingsList.ToArray();
			PakageSettings[] array2 = array;
			foreach (PakageSettings pakageSettings in array2)
			{
				GameObject gameObject = Object.Instantiate(CoinPrefab, pakageSettings.Position, Quaternion.identity) as GameObject;
				gameObject.transform.SetParent(base.transform);
				gameObject.GetComponent<HidenPakage>().PakageType = pakageSettings.PakageType;
				gameObject.GetComponent<HidenPakage>().PakageIndex = pakageSettings.PakageIndex;
			}
		}
		else
		{
			BCWDebug.LogError("Create pakage list for this level!");
		}
	}

	public void SaveEdit()
	{
		if ((bool)pakageList)
		{
			pakageList.packageSettingsList.Clear();
			HidenPakage[] array = Object.FindObjectsOfType<HidenPakage>();
			HidenPakage[] array2 = array;
			foreach (HidenPakage hidenPakage in array2)
			{
				if (hidenPakage.PakageIndex != 0)
				{
					pakageList.AddNewSettings(hidenPakage.PakageType, hidenPakage.PakageIndex, hidenPakage.transform.position);
					Object.DestroyImmediate(hidenPakage.gameObject);
				}
			}
			listsHolder.RefreshAllCoins();
		}
		else
		{
			BCWDebug.LogError("Create pakage list for this level!");
		}
	}

	public void EndEdit()
	{
		if ((bool)pakageList)
		{
			HidenPakage[] array = Object.FindObjectsOfType<HidenPakage>();
			HidenPakage[] array2 = array;
			foreach (HidenPakage hidenPakage in array2)
			{
				if (hidenPakage.PakageIndex != 0)
				{
					Object.DestroyImmediate(hidenPakage.gameObject);
				}
			}
		}
		else
		{
			BCWDebug.LogError("Create pakage list for this level!");
		}
	}

	private int GetNewIndex()
	{
		if ((bool)pakageList)
		{
			int num = 1;
			PakageSettings[] allPakages = pakageList.packageSettingsList.ToArray();
			bool flag = IsIndexExist(num, allPakages);
			while (flag)
			{
				num++;
				flag = IsIndexExist(num, allPakages);
			}
			return num;
		}
		BCWDebug.LogError("Create pakage list for this level!");
		return 0;
	}

	private bool IsIndexExist(int index, PakageSettings[] allPakages)
	{
		foreach (PakageSettings pakageSettings in allPakages)
		{
			if (pakageSettings.PakageIndex == index)
			{
				return true;
			}
		}
		return false;
	}

	public void SetAllAsNotCollected()
	{
		if (!listsHolder)
		{
			return;
		}
		foreach (PakageSettingsList allLevelsPakageList in listsHolder.allLevelsPakageLists)
		{
			if (!(allLevelsPakageList != null))
			{
				continue;
			}
			foreach (PakageSettings packageSettings in allLevelsPakageList.packageSettingsList)
			{
				BCWDebug.Log(string.Concat("Uncollect - ", allLevelsPakageList.levelNme, " ", packageSettings.PakageType, " ", packageSettings.PakageIndex));
				string key = string.Concat(allLevelsPakageList.levelNme, " ", packageSettings.PakageType, " ", packageSettings.PakageIndex);
				BCWStorage.Set(key, false);
			}
		}
	}
}
